#
# Copyright (c) 2009-2012 Intel Corporation
# written by Roman Dementiev
#

OBJDIR  = ../obj
BINDIR  = ../bin

all: dirs $(BINDIR)/pcm-power.x $(BINDIR)/pcm.x $(BINDIR)/pcm-sensor.x $(BINDIR)/pcm-msr.x $(BINDIR)/pcm-memory.x

CC  = g++ -Wall
OPT = -g -O3 

UNAME:=$(shell uname)

ifeq ($(UNAME), Linux)
LIB= -lpthread -lrt
endif
ifeq ($(UNAME), Darwin)
LIB= -lpthread -lPcmMsr
endif
ifeq ($(UNAME), FreeBSD)
LIB= -lpthread
endif


$(OBJDIR)/msr.o: msr.hpp msr.cpp 
	$(CC) $(OPT) -c msr.cpp -o $@

$(OBJDIR)/pci.o: pci.hpp pci.cpp
	$(CC) $(OPT) -c pci.cpp -o $@

$(OBJDIR)/pcm-msr.o: msr.hpp msr.cpp pcm-msr.cpp
	$(CC) $(OPT) -c pcm-msr.cpp -o $@

$(OBJDIR)/pcm-memory.o: pcm-memory.cpp msr.hpp pci.hpp cpucounters.hpp types.hpp width_extender.hpp
	$(CC) $(OPT) -c pcm-memory.cpp -o $@

$(OBJDIR)/cpucounters.o: msr.hpp msr.cpp cpucounters.hpp cpucounters.cpp types.hpp width_extender.hpp 
	$(CC) $(OPT) -c cpucounters.cpp -o $@

$(BINDIR)/msrtest.x: msrtest.cpp $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o cpucounters.hpp  msr.hpp  types.hpp
	$(CC) $(OPT) msrtest.cpp -o $(BINDIR)/msrtest.x $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o

$(OBJDIR)/cpucounterstest.o: cpucounterstest.cpp cpucounters.hpp pci.hpp msr.hpp  types.hpp
	$(CC) $(OPT) -c cpucounterstest.cpp -o $@

$(OBJDIR)/pcm-power.o: pcm-power.cpp msr.hpp types.hpp pci.hpp cpucounters.hpp
	$(CC) $(OPT) -c pcm-power.cpp -o $@

$(OBJDIR)/realtime.o: realtime.cpp cpucounters.hpp  msr.hpp  types.hpp
	$(CC) $(OPT) -c realtime.cpp -o $@

$(BINDIR)/pcm.x: $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o $(OBJDIR)/cpucounterstest.o $(OBJDIR)/pci.o
	$(CC) $(OPT) $(OBJDIR)/msr.o $(OBJDIR)/pci.o $(OBJDIR)/cpucounters.o $(OBJDIR)/cpucounterstest.o -o $(BINDIR)/pcm.x $(LIB)

$(BINDIR)/pcm-power.x: $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o $(OBJDIR)/pci.o $(OBJDIR)/pcm-power.o
	$(CC) $(OPT) $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o $(OBJDIR)/pci.o $(OBJDIR)/pcm-power.o -o $(BINDIR)/pcm-power.x $(LIB)

$(BINDIR)/pcm-msr.x: $(OBJDIR)/msr.o $(OBJDIR)/pcm-msr.o
	$(CC) $(OPT) $(OBJDIR)/msr.o $(OBJDIR)/pcm-msr.o -o $(BINDIR)/pcm-msr.x $(LIB)

$(BINDIR)/realtime.x: $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o $(OBJDIR)/realtime.o
	$(CC) $(OPT) $(OBJDIR)/pci.o $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o $(OBJDIR)/realtime.o -o $(BINDIR)/realtime.x $(LIB)

$(BINDIR)/pcm-memory.x: $(OBJDIR)/msr.o $(OBJDIR)/pcm-memory.o $(OBJDIR)/pci.o $(OBJDIR)/cpucounters.o
	$(CC) $(OPT) $(OBJDIR)/msr.o $(OBJDIR)/pci.o $(OBJDIR)/cpucounters.o $(OBJDIR)/pcm-memory.o -o $(BINDIR)/pcm-memory.x $(LIB)

$(OBJDIR)/pcm-sensor.o: pcm-sensor.cpp cpucounters.hpp cpuasynchcounter.hpp msr.hpp  types.hpp
	$(CC) $(OPT) -c pcm-sensor.cpp -o $@

$(BINDIR)/pcm-sensor.x: $(OBJDIR)/msr.o $(OBJDIR)/cpucounters.o $(OBJDIR)/pcm-sensor.o $(OBJDIR)/pci.o
	$(CC) $(OPT) $(OBJDIR)/msr.o $(OBJDIR)/pci.o $(OBJDIR)/cpucounters.o $(OBJDIR)/pcm-sensor.o -o $(BINDIR)/pcm-sensor.x $(LIB)

dirs:
	mkdir -p $(BINDIR)
	mkdir -p $(OBJDIR)

clean:
	rm -rf *.x *.o *~
	rm -f $(OBJDIR)/*.o
	rm -f $(BINDIR)/*.x

